package cn.com.bdqn.controller;

import cn.com.bdqn.entity.SyEmpAreaPower;
import cn.com.bdqn.service.SyEmpAreaPowerService;
import cn.com.bdqn.utils.ReturnData;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/syEmpAreaPower")
@Api(tags = "员工辖区权限管理接口")
public class SyEmpAreaPowerController {

    @Autowired
    private SyEmpAreaPowerService syEmpAreaPowerService;

    @Operation(summary = "根据员工ID查询员工辖区权限信息")
    @PostMapping("/queryByEmpId")
    public ReturnData queryByEmpId(Long empId){
        try {
            List<SyEmpAreaPower> syEmpAreaPowers = syEmpAreaPowerService.queryByEmpId(empId);
            return ReturnData.ok().put("syEmpAreaPowers",syEmpAreaPowers);
        }catch (Exception e){
            return ReturnData.currError("根据员工ID查询员工辖区权限信息数据库异常");
        }
    }

    @Operation(summary = "根据员工ID删除员工辖区权限信息")
    @PostMapping("/delSyEmpAreaPower")
    public ReturnData delSyEmpAreaPower(Long empId){
        int result = syEmpAreaPowerService.delSyEmpAreaPower(empId);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("根据员工ID删除员工辖区权限信息数据库异常");
        }
    }


    @Operation(summary = "新增员工辖区权限信息")
    @PostMapping("/addSyEmpAreaPower")
    public ReturnData addSyEmpAreaPower(List empIds,List areaId){
        SyEmpAreaPower syEmpAreaPower = new SyEmpAreaPower();

        int result = 0;
        for (Object empId : empIds) {
            syEmpAreaPower.setEmpId((Long) empId);
            for (Object o : areaId) {
                syEmpAreaPower.setAreaId((Long) o);
                syEmpAreaPowerService.addSyEmpAreaPower(syEmpAreaPower);
                result++;
            }
        }

        if (result > (empIds.size() * areaId.size())){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("新增员工辖区权限信息数据库异常");
        }
    }

}
